// 手写函数实现
// new A().log(1).wait(2).wait(2).log(1).wait(2)

class A {
  constructor() {
    this.tasks = [];
    setTimeout(() => {
      this.next();
    },0)
  }
  next() {
    let task = this.tasks.shift();
    task && task();
  }

  log(num) {
    let task = () => {
      console.log(num);
      this.next();
    }
    this.tasks.push(task);
    return this;
  }
  wait(time) {
    let task = () => {
      setTimeout(() => {
        console.log(`wait ${time}`);
        this.next();
      }, time * 1000)
    }
    this.tasks.push(task)
    return this;
  }
}

new A().log(1).wait(2).wait(2).log(1).wait(2)